package com.jiuzhe.commercial.ui.my;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.jiuzhe.commercial.R;
import com.jiuzhe.commercial.adapter.AccountInfoAdapter;
import com.jiuzhe.commercial.base.BaseActivity;
import com.jiuzhe.commercial.bean.AccountListBean;
import com.jiuzhe.commercial.constant.Constants;
import com.jiuzhe.commercial.http.CallBack;
import com.jiuzhe.commercial.http.HttpClient;
import com.jiuzhe.commercial.utils.SPUtil;
import com.jiuzhe.commercial.utils.UIUtils;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * Created by huanghao on 2018/6/22.
 */

public class AccountListActivity extends BaseActivity {

    @BindView(R.id.listview)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;
    @BindView(R.id.iv_not)
    ImageView ivNot;
    private List<AccountListBean> list = new ArrayList<>();
    private AccountInfoAdapter mAccountAdapter;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_account);
        setTitle("收入明细");
        intData();
    }

    private void intData() {
        recyclerView.setLayoutManager(new LinearLayoutManager(AccountListActivity.this));
        //设置Item增加、移除动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        mAccountAdapter = new AccountInfoAdapter(list);
        recyclerView.setAdapter(mAccountAdapter);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        loadData();
                        refreshLayout.finishRefresh();
                    }
                },1500);
            }
        });

        /*refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshLayout.finishLoadmoreWithNoMoreData();
            }
        });*/
    }

    @Override
    protected void loadData() {
        HashMap<String, String> map = new HashMap<>();
        map.put("merchantId", SPUtil.getString(AccountListActivity.this, Constants.MERCHANTID));
        String token = SPUtil.getString(AccountListActivity.this, Constants.TOKEN);
        String id = SPUtil.getString(AccountListActivity.this, Constants.ID);
        String autoSignStr = Constants.HOTELORDERS.substring(Constants.HOTELORDERS.lastIndexOf("0/") + 1) + "?token=";
        String signStr = UIUtils.Md5(autoSignStr + token);
        map.put("token", id);
        map.put("sign", signStr);
        HttpClient.post(this, Constants.HOTELORDERS, map, new CallBack<List<AccountListBean>>() {
            @Override
            public void onSuccess(List<AccountListBean> result) {
                list.clear();
                list.addAll(result);
                if (list != null && list.size() > 0){
                    mAccountAdapter.notifyDataSetChanged();
                }else {
                    ivNot.setVisibility(View.VISIBLE);
                }
            }
        });
    }
}
